package fdoj.eventInvitations.dao;

import java.util.List;


import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import fdoj.eventInvitations.entity.Event;
import fdoj.eventInvitations.entity.Person;

/**
 * Klasa koja implementira CRUD operacije za entitet {@link Event} 
 */
@Stateless
@Local(EventDao.class)
public class EventDaoBean extends GenericDaoBean<Event, Integer> implements EventDao{
	public List<Event> invitedByMail(String email){
		List<Event> retVal = null;
		try {
			Query q = em.createQuery("SELECT e FROM Event e, Contact c, Invantation i" +
					" WHERE c.email like :email AND c.id=i.eventCall.id AND i.event.id=e.id");
			q.setParameter("email", email);
			retVal= (List<Event>) q.getResultList();
		} catch (NoResultException ex) {

		}
		return retVal;
	}
	public void deleteById(Integer id) {
		Query q = em.createQuery("UPDATE Event SET canceled=TRUE WHERE id=:id");
		q.setParameter("id", id);
		q.executeUpdate();
	}
	
	public List<Event> eventByMaker(Integer id){
		List<Event> retVal=null;
		try{
			Query q=em.createQuery("SELECT e FROM Event e WHERE e.owner.id=:id ORDER BY e.startTime DESC");
			q.setParameter("id", id);
			retVal=(List<Event>)q.getResultList();
		}catch(NoResultException ex){
			
		}
		return retVal;
	}
}
